package OnlineShopping;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import OnlineShopping.Customer;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Admin admin = new Admin("admin01", "admin", "ynuinfo#777", "admin@example.com", "1234567890", "2023-01-01", "管理员", 0, new HistoryRecord());

        while (true) {
            System.out.println("\n欢迎来到在线购物系统");
            System.out.println("1. 登录为管理员");
            System.out.println("2. 登录为客户");
            System.out.println("3. 注册客户");
            System.out.println("4. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 处理换行符

            if (choice == 1) {
                // 管理员登录
                System.out.print("请输入管理员用户名: ");
                String username = scanner.nextLine();
                System.out.print("请输入管理员密码: ");
                String password = scanner.nextLine();

                if (admin.login(username, password)) {
                    System.out.println("管理员登录成功！");
                    adminMenu(scanner, admin);
                } else {
                    System.out.println("管理员用户名或密码错误。");
                }
            } else if (choice == 2) {
                // 客户登录
                Customer customer = new Customer();
                System.out.print("请输入客户用户名: ");
                String username = scanner.nextLine();
                System.out.print("请输入客户密码: ");
                String password = scanner.nextLine();

                if (customer.login(username, password)) {
                    System.out.println("客户登录成功！");
                    customerMenu(scanner, customer);
                } else {
                    System.out.println("客户用户名或密码错误。");
                }
            } else if (choice == 3) {
                // 注册客户
                System.out.print("请输入新客户用户名: ");
                String newUsername = scanner.nextLine();
                System.out.print("请输入新客户密码: ");
                String newPassword = scanner.nextLine();
                System.out.print("请输入邮箱: ");
                String email = scanner.nextLine();
                System.out.print("请输入手机号: ");
                String phone = scanner.nextLine();

                if (isValidUsername(newUsername) && isValidPassword(newPassword)) {
                    Customer customer = new Customer();
                    customer.register(newUsername, newPassword, email, phone);
                    System.out.println("客户注册成功！");
                } else {
                    System.out.println("用户名或密码不符合规则，请检查。");
                }
            } else if (choice == 4) {
                System.out.println("感谢使用在线购物系统，再见！");
                break; // 退出系统
            } else {
                System.out.println("无效的选择，请重试。");
            }
        }
        scanner.close();
    }

    private static void adminMenu(Scanner scanner, Admin admin) {
        while (true) {
            System.out.println("\n管理员菜单");
            System.out.println("1. 修改自身密码");
            System.out.println("2. 重置客户密码");
            System.out.println("3. 列出所有客户信息");
            System.out.println("4. 删除客户信息");
            System.out.println("5. 查询客户信息");
            System.out.println("6. 列出所有商品信息");
            System.out.println("7. 添加商品信息");
            System.out.println("8. 修改商品信息");
            System.out.println("9. 删除商品信息");
            System.out.println("10. 查询商品信息");
            System.out.println("11. 退出管理员");
            System.out.print("请选择操作: ");
            int adminChoice = scanner.nextInt();
            scanner.nextLine(); // 处理换行符

            switch (adminChoice) {
                case 1:
                    System.out.print("请输入新密码: ");
                    String newPassword = scanner.nextLine();
                    admin.setPassword(newPassword);
                    System.out.println("密码修改成功！");
                    break;
                case 2:
                    System.out.print("请输入要重置密码的客户ID: ");
                    String customerId = scanner.nextLine();
                    System.out.print("请输入新密码: ");
                    String resetPassword = scanner.nextLine();
                    admin.resetCustomerPassword(customerId, resetPassword);
                    break;
                case 3:
                    admin.listCustomers();
                    break;
                case 4:
                    System.out.print("请输入要删除的客户ID: ");
                    String deleteCustomerId = scanner.nextLine();
                    admin.deleteCustomer(deleteCustomerId);
                    break;
                case 5:
                    System.out.print("请输入客户ID或用户名进行查询: ");
                    String queryInput = scanner.nextLine();
                    admin.queryCustomer(queryInput);
                    break;
                case 6:
                    admin.listProducts();
                    break;
                case 7:
                    System.out.print("请输入商品ID: ");
                    String productId = scanner.nextLine();
                    System.out.print("请输入商品名称: ");
                    String productName = scanner.nextLine();
                    System.out.print("请输入商品价格: ");
                    double productPrice = scanner.nextDouble();
                    admin.addProduct(productId, productName, productPrice);
                    break;
                case 8:
                    System.out.print("请输入要修改的商品ID: ");
                    String modifyProductId = scanner.nextLine();
                    System.out.print("请输入新商品名称: ");
                    String newProductName = scanner.nextLine();
                    System.out.print("请输入新商品价格: ");
                    double newProductPrice = scanner.nextDouble();
                    admin.modifyProduct(modifyProductId, newProductName, newProductPrice);
                    break;
                case 9:
                    System.out.print("请输入要删除的商品ID: ");
                    String deleteProductId = scanner.nextLine();
                    admin.deleteProduct(deleteProductId);
                    break;
                case 10:
                    System.out.print("请输入要查询的商品ID: ");
                    String queryProductId = scanner.nextLine();
                    admin.queryProduct(queryProductId);
                    break;
                case 11:
                    System.out.println("退出管理员。");
                    return; // 退出管理员菜单
                default:
                    System.out.println("无效的选择，请重试。");
                    break;
            }
        }
    }

    private static void customerMenu(Scanner scanner, Customer customer) {
        while (true) {
            System.out.println("\n客户菜单");
            System.out.println("1. 修改自身密码");
            System.out.println("2. 忘记密码");
            System.out.println("3. 添加商品到购物车");
            System.out.println("4. 从购物车中移除商品");
            System.out.println("5. 修改购物车中的商品");
            System.out.println("6. 查看购物车");
            System.out.println("7. 结账");
            System.out.println("8. 查看购物历史");
            System.out.println("9. 退出客户");
            System.out.print("请选择操作: ");
            int customerChoice = scanner.nextInt();
            scanner.nextLine(); // 处理换行符

            switch (customerChoice) {
                case 1:
                    System.out.print("请输入新密码: ");
                    String newPassword = scanner.nextLine();
                    customer.editPassword(newPassword);
                    System.out.println("密码修改成功！");
                    break;
                case 2:
                    System.out.print("请输入用户名: ");
                    String username = scanner.nextLine();
                    System.out.print("请输入注册邮箱: ");
                    String email = scanner.nextLine();
                    customer.resetPassword(username, email);
                    break;
                case 3:
                    System.out.print("请输入商品ID: ");
                    String productId = scanner.nextLine(); // 用户输入商品ID
                    System.out.print("请输入数量: ");
                    int quantity = scanner.nextInt(); // 用户输入数量
                    customer.addToCart(productId, quantity); // 调用客户的 addToCart 方法
                    break;
                case 4:
                    System.out.print("请输入要移除的商品ID: ");
                    String removeProductId = scanner.nextLine();
                    customer.removeFromCart(removeProductId);
                    break;
                case 5:
                    System.out.print("请输入商品ID: ");
                    String modifyProductId = scanner.nextLine();
                    System.out.print("请输入新数量: ");
                    int newQuantity = scanner.nextInt();
                    customer.modifyProductIn(modifyProductId, newQuantity);
                    break;
                case 6:
                    customer.shoppingCart.viewCart();
                    break;
                case 7:
                    customer.checkout();
                    break;
                case 8:
                    customer.viewShoppingHistory();
                    break;
                case 9:
                    System.out.println("退出客户。");
                    return; // 退出客户菜单
                default:
                    System.out.println("无效的选择，请重试。");
                    break;
            }
        }
    }

    private static boolean isValidUsername(String username) {
        return username.length() >= 5;
    }

    private static boolean isValidPassword(String password) {
        return password.length() > 8 && password.matches(".*[a-zA-Z].*") && password.matches(".*\\d.*") && password.matches(".*[^a-zA-Z0-9].*");
    }
}